package com.bwie.redkids.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class GsonUtils {
	/**
	 * @param url
	 * @return ����һ����ַ������������
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private final static String TAG = "login";

	public static String getData(String url) throws ClientProtocolException,
			IOException {
		// ��һ��������HttpClient����
		HttpClient client = new DefaultHttpClient();
		// �ڶ�����������������Ķ���,�����Ƿ��ʵķ�������ַ
		HttpGet httpGet = new HttpGet(url);
		// ��������ִ�����󣬻�ȡ��������������Ӧ����
		HttpResponse httpResponse = client.execute(httpGet);
		// ���Ĳ��������Ӧ��״̬�Ƿ����������״̬���ֵ��200��ʾ����
		if (httpResponse.getStatusLine().getStatusCode() == 200) {
			// ���岽������Ӧ������ȡ�����ݣ��ŵ�entity����
			HttpEntity entity = httpResponse.getEntity();
			String response = EntityUtils.toString(entity, "utf-8");// ��entity���е�����ת��Ϊ�ַ���
			return response;

		}
		return null;
	}

	public static String loginByPost(String loginUrl, String username,
			String password) {
		String result = null;
		// ͨ��HttpPost��װPOSt����
		HttpPost req = new HttpPost(loginUrl);
		HttpParams connParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(connParams, 5 * 1000);
		HttpConnectionParams.setSoTimeout(connParams, 5 * 1000);
		// ��װ���������Post��ʽ���еĲ���
		List<NameValuePair> reqParams = new ArrayList<NameValuePair>();
		NameValuePair usernameParam = new BasicNameValuePair("username",
				username);
		NameValuePair passwordParam = new BasicNameValuePair("password",
				password);
		reqParams.add(usernameParam);
		reqParams.add(passwordParam);
		try {
			// �����������װ��HttpEntity���˴�����ָ����������ı����ʽ
			HttpEntity entityParams = new UrlEncodedFormEntity(reqParams,
					"utf-8");
			// �����������
			req.setEntity(entityParams);

			HttpClient client = new DefaultHttpClient(connParams);
			HttpResponse resp = client.execute(req);
			if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = resp.getEntity();
				// ��HttpEntityת����String
				result = EntityUtils.toString(entity, "utf-8");
			} else {
				return "��¼ʧ��";
			}
		} catch (ClientProtocolException e) {
			Log.e(TAG, "" + e.getMessage());
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			Log.e(TAG, "" + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			Log.e(TAG, "" + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

}
